package com.horus.entities.dao.hibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

public class HibernateDao {
	
	protected SessionFactory sessionFactory;
	protected Session session;
	protected Transaction transaction;
	
	public void commitTransaction() {

		this.transaction.commit();
	}

	public void beginTransaction() {

		if(!isSessionOpen()){
			openConnection();
		}
		this.transaction = session.beginTransaction();
	}

	public void openConnection(){
		
		this.session = sessionFactory.openSession();
	}
	public void closeConnection() {

		if(isTransactionActive()){
			this.transaction.commit();
		}
		
		this.session.close();
	}

	public boolean isSessionOpen() {

		return this.session.isOpen();
	}

	public boolean isTransactionActive() {

		return this.transaction.isActive();
	}

}
